package com.tanhua.manager.service;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mogo.Comment;
import com.tanhua.domain.mogo.Publish;
import com.tanhua.domain.mogo.Video;
import com.tanhua.domain.vo.CommentVO;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.domain.vo.PublishVO;
import com.tanhua.domain.vo.VideoVO;
import com.tanhua.dubbo.api.*;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserService {
    @Reference
    private VideoApi videoApi;
    @Reference
    private CommentApi commentApi;
    @Reference
    private PublishApi publishApi;
    @Reference
    private UserInfoApi userInfoApi;

    //用户管理----用户分页管理
    public ResponseEntity findUserList(int page, int pagesize) {
        //分页查询用户列表
     IPage<UserInfo> iPage= userInfoApi.findUserList(page,pagesize);
     //转换成PageResult
        PageResult<UserInfo> pageResult = new PageResult<>();
        pageResult.setPage(page);
        pageResult.setPagesize(pagesize);
        pageResult.setPages((int) iPage.getPages());
        pageResult.setCounts((int) iPage.getTotal());
        pageResult.setItems(iPage.getRecords());
        //返回结果
        return ResponseEntity.ok(pageResult);

    }

    // 用户管理-查看用户详情
    public ResponseEntity findUserById(Long userId) {
        UserInfo userInfo = userInfoApi.findById(userId);
        return ResponseEntity.ok(userInfo);

    }
    /**
     * 分页查询用户的动态列表
     * @param page 页码
     * @param pagesize 查询几条
     * @param uid 用户id
     * @param state 动态的审核状态
     */
    public ResponseEntity findPublishList(int page, int pagesize, Long uid, String state) {
        //分页查询某用户发布的动态列表
        PageResult<Publish> pageResult=publishApi.findPublishList(page,pagesize,uid,state);
        //转换成Vo
        List<Publish> publishList = pageResult.getItems();
        List<PublishVO> voList = new ArrayList<>();
        for (Publish publish : publishList) {
            PublishVO vo = new PublishVO();

            //查询动态的发布人信息，封装到vo对象
            UserInfo userInfo = userInfoApi.findById(publish.getUserId());
            BeanUtils.copyProperties(userInfo,vo);
            vo.setTags(userInfo.getTags().split(","));

            //把publish封装到vo对象
            BeanUtils.copyProperties(publish,vo);
            vo.setId(publish.getId().toHexString());
            vo.setImageContent(publish.getMedias().toArray(new String[]{}));
            vo.setDistance("50米");
            vo.setCreateDate(new DateTime(publish.getCreated()).toString("yyyy-MM-dd HH:mm:ss"));
            voList.add(vo);

        }
        //构造返回值
        PageResult<PublishVO> voPageResult = new PageResult<>();
        BeanUtils.copyProperties(pageResult,voPageResult);
        voPageResult.setItems(voList);


        return ResponseEntity.ok(voPageResult);
    }
    /**
     * 动态管理-查看单条动态详情
     * GET /manage/messages/:id
     */
    public ResponseEntity findPublishById(String publishId) {
        //查询publishId对应的动态
        Publish publish = publishApi.findById(publishId);

        //转换成vo
        PublishVO vo = new PublishVO();
        //查询动态发布人信息 封装到vo对象里
        UserInfo userInfo = userInfoApi.findById(publish.getUserId());
        BeanUtils.copyProperties(userInfo,vo);
        vo.setTags(userInfo.getTags().split(","));
        //把publish对象封装到vo里
        BeanUtils.copyProperties(publish,vo);
        vo.setId(publish.getId().toHexString());
        vo.setImageContent(publish.getMedias().toArray(new String[ ]{}));
        vo.setDistance("50米");
        vo.setCreateDate(new DateTime(publish.getCreated()).toString("yyyy-MM-dd HH:mm:ss"));

        return ResponseEntity.ok(vo);
    }

    /**
     * 评论列表
     * GET /manage/messages/comments
     */
    public ResponseEntity findCommentList(int page, int pagesize, String messageId) {
        //查询评论列表
        PageResult<Comment> pageResult=commentApi.findCommentList(page,pagesize,messageId);
        //转换成vo
        List<Comment> commentList = pageResult.getItems();
        List<CommentVO> voList = new ArrayList<>();
        for (Comment comment : commentList) {
            CommentVO vo = new CommentVO();
            vo.setId(comment.getId().toHexString());
            vo.setContent(comment.getContent());
            vo.setCreateDate(new DateTime(comment.getCreated()).toString("yyyy-MM-dd HH:mm:ss"));
            UserInfo userInfo = userInfoApi.findById(comment.getUserId());
            vo.setNickname(userInfo.getNickname());
            vo.setAvatar(userInfo.getAvatar());
            voList.add(vo);

        }
        //构造返回值
        PageResult<CommentVO> voPageResult = new PageResult<>();
        BeanUtils.copyProperties(pageResult,voPageResult);
        voPageResult.setItems(voList);
        return ResponseEntity.ok(voPageResult);
    }

    /**
     * 用户管理-分页查看用户的视频列表
     * GET /manage/videos
     */
    public ResponseEntity findVideoList(int page, int pagesize, Long uid) {
        //查询视频列表
        PageResult<Video> pageResult= videoApi.fingVideoList(page,pagesize,uid);
        //转换成vo
        List<Video> videoList = pageResult.getItems();
        List<VideoVO> voList = new ArrayList<>();
        for (Video video : videoList) {
            VideoVO vo = new VideoVO();
            //封装视频发布者的信息
            UserInfo userInfo = userInfoApi.findById(video.getUserId());
             vo.setUserId(userInfo.getId());
             vo.setAvatar(userInfo.getAvatar());
             vo.setNickname(userInfo.getNickname());
             //封装视频信息
            vo.setId(video.getId().toHexString());
            vo.setCover(video.getPicUrl());
            vo.setVideoUrl(video.getVideoUrl());
            vo.setSignature(video.getText());

            vo.setLikeCount(0);
            vo.setCommentCount(0);
            voList.add(vo);
        }
        //构造返回值
        PageResult<VideoVO> voPageResult = new PageResult<>();
          BeanUtils.copyProperties(pageResult,voPageResult);
          voPageResult.setItems(voList);

       return ResponseEntity.ok(voPageResult);
    }
}
